package com.swpu.service_food.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.swpu.service_food.entity.FoodRecord;
import com.swpu.service_food.entity.vo.FoodRecordVo;
import com.swpu.service_food.entity.vo.FoodVo;
import com.swpu.service_food.mapper.FoodRecordMapper;
import com.swpu.service_food.service.FoodRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chaichai
 * @since 2024-01-27
 */
@Service
public class FoodRecordServiceImpl extends ServiceImpl<FoodRecordMapper, FoodRecord> implements FoodRecordService {

    @Override
    public void addFoodRecord(FoodVo foodVo, String openid) {
        FoodRecord foodRecord = new FoodRecord();
        foodRecord.setFoodId(foodVo.getId());
        foodRecord.setUserId(openid);
        foodRecord.setDietType(foodVo.getDietType());
        foodRecord.setRecordTime(LocalDateTime.now());
        foodRecord.setRealWeight(foodVo.getRealWeight());
        boolean isSave = this.save(foodRecord);
        if (!isSave) throw new RuntimeException("添加用户饮食数据失败");

    }

    @Override
    public List getFoodRecordeByType(Integer dietType, String openid) {
        LocalDateTime startTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0, 0));
        LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(23, 59, 59));
        List<FoodRecordVo> foodRecordVoList = baseMapper.getFoodRecordByType(dietType, openid,startTime,endTime);
        return foodRecordVoList;
    }

    @Override
    public void deleteFoodRecordById(String id) {
        boolean isRemove = this.removeById(id);
        if (!isRemove) throw new RuntimeException("删除用户饮食数据失败");
    }

    @Override
    public Integer getFoodRecordNum(String openid) {
        LocalDateTime startTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0, 0));
        LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(23, 59, 59));

        LambdaQueryWrapper<FoodRecord> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(FoodRecord::getUserId,openid);
        queryWrapper.ge(FoodRecord::getRecordTime, startTime).lt(FoodRecord::getRecordTime, endTime);

        int count = this.count(queryWrapper);
        return count;
    }

    @Override
    public Integer getFoodCaloriesByDate(String openid, DateTime queryDate) {
        DateTime beginOfDay = DateUtil.beginOfDay(queryDate);
        DateTime endOfDay = DateUtil.endOfDay(queryDate);
        List<FoodRecordVo> foodRecordVos = baseMapper.getFoodRecordByDate(openid, beginOfDay, endOfDay);
        int calories=0;
        for (FoodRecordVo foodRecordVo : foodRecordVos) {
                calories+=foodRecordVo.getCalories()*1.0/foodRecordVo.getWeight()*foodRecordVo.getRealWeight();
        }
        return calories;
    }
}
